package com.huluobo.a2205parkdemo.visitor

import android.app.DatePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2205parkdemo.R
import com.huluobo.a2205parkdemo.databinding.ActivityAddVisitorBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding, VisitorViewModel>() {
    private var startTime: Int = 0
    private var endTime: Int = 0
    private var time = ""
    private var carNum = ""
    private var desc = ""
    override fun initData() {
        vm.addVisitorSuccess.observe(this) {
            ToastUtils.showLong("添加来访成功")
            finish()
        }
    }

    override fun initView() {
        binding.addVisitorHeader.headerTitle.text = "添加来访"
        binding.addVisitorHeader.headerBack.visibility = View.VISIBLE
        binding.addVisitorHeader.headerBack.setOnClickListener { finish() }

        binding.addVisitorStartTimeBtn.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { view, year, month, dayOfMonth ->
                    binding.addVisitorStartTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    startTime = dayOfMonth
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorEndTimeBtn.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(
                this,
                { view, year, month, dayOfMonth ->
                    binding.addVisitorEndTimeNowTv.text = "$year-${month + 1}-$dayOfMonth"
                    endTime = dayOfMonth
                    val tampTime = endTime - startTime
                    binding.addVisitorTimeNowTv.text = "${tampTime}天"
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            ).show()
        }

        binding.addVisitorCommitBtn.setOnClickListener {
            carNum = binding.addVisitorCarNumEt.text.toString()
            desc = binding.addVisitorDescEt.text.toString()

            if (binding.addVisitorStartTimeNowTv.text.isNotEmpty() &&
                binding.addVisitorEndTimeNowTv.text.isNotEmpty() &&
                binding.addVisitorTimeNowTv.text.isNotEmpty() &&
                carNum.isNotEmpty() && desc.isNotEmpty()
            ) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_VISITOR_START_TIME] = binding.addVisitorStartTimeNowTv.text.toString()
                map[Const.PARAM_VISITOR_END_TIME] = binding.addVisitorEndTimeNowTv.text.toString()
                map[Const.PARAM_VISITOR_TIME] = binding.addVisitorTimeNowTv.text.toString()
                map[Const.PARAM_CAR_NUM] = carNum
                map[Const.PARAM_VISITOR_DESC] = desc

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.addVisitor(body)


            } else {
                ToastUtils.showLong("来访内容不能有空值")
            }
        }
    }
}